﻿using DMSkin.Core;
using Dt261.ble;
using Dt261.Views;
using GalaSoft.MvvmLight;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Dt261.ViewModel
{
    class MainWindowViewModel:  WindowOSDAVM
    {
        ByteBuffer buffer =  ByteBuffer.Allocate(1024*1024);//1M


        public string HospitalTitleText { get => GlobalData.HospitalTitleText; set {  RaisePropertyChanged(); } }

        //ArrayList recvlist = new ArrayList();
        //System.Timers.Timer t;
        public MainWindowViewModel()
        {
            SerialPortModel.UTF8Enable = true;
            SerialPortModel.BaudRate = 115200;

          

            
            //DataBits = 8;
            //StopBits = StopBits.One;
            //Parity = Parity.None;
            navchange("1");
            DepictInfo = string.Format(CultureInfos, "欢迎使用");

            /* 数据接收事件 */
            SerialPortBase.DataReceived -= SerialPortDataReceived;
            SerialPortBase.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceivedXIN);
            //t = new System.Timers.Timer(100);//实例化Timer类，设置间隔时间为10000毫秒；
            //t.Elapsed += new System.Timers.ElapsedEventHandler(UpdatePort);//到达时间的时候执行事件；
            //t.AutoReset = true;//设置是执行一次（false）还是一直执行(true)；
            //t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件；
            //t.Start(); //启动定时器
            //上面初始化代码可以写到构造函数中

            DMSkin.Core.Broadcast.RegisterBroadcast<string>("UpdateTitle", (obj) =>
            {
                HospitalTitleText= GlobalData.HospitalTitleText;
            });
        }

        #region 数据接收事件实现
        private void SerialPortDataReceivedXIN(object sender, SerialDataReceivedEventArgs e)
        {
            if ((SerialPort)sender == null)
            {
                return;
            }

            SerialPort _SerialPort = (SerialPort)sender;

            int _BytesToRead = _SerialPort.BytesToRead;
            byte[] _RecvData = new byte[_BytesToRead];

            _SerialPort.Read(_RecvData, 0, _BytesToRead);


            if (_RecvData[_BytesToRead - 1] != 0xFF) // ErCommandAction.CommandAction_END)
            {
                buffer.WriteBytes(_RecvData, _BytesToRead);
            }
            else
            {

                buffer.WriteBytes(_RecvData, _BytesToRead);
       
                
                ErCommand command = new ErCommand();
                command.parseFromHexData(buffer.ToArray());

                //此处应该根据action来操作
         
                buffer.Clear();
            }

            Console.WriteLine(""+ _SerialPort.Encoding.GetString(_RecvData));

            DepictInfo = string.Format(CultureInfos, "接收到" + _BytesToRead +"，当前buffer:"+ buffer.WriterIndex);
        }
        #endregion
        //~MainWindowViewModel()
        //{
        //    t.Stop();
        //}

        //private void UpdatePort(object source, System.Timers.ElapsedEventArgs e)
        //{
        //    SerialPortModel.SerialPortDataContext();
        //}

        private UserPageEs up;
        private SettingPageEs sp;
        private ResultPageEs dw;
        private AboutPage ap;
        private ChangePsw cp;
        private PureToneCorrect cor;

        private HostPage hp;

        public UserPageEs Up {
            get {
                if(up == null)
                {
                    up = new UserPageEs();
                }
                return up;
            }
            set => up = value; }

        public PureToneCorrect Cor
        {
            get
            {
                if (cor == null)
                {
                    cor = new PureToneCorrect();
                }
                return cor;
            }
            set => cor = value;
        }

        public ICommand NavigationCommand => new DelegateCommand(obj => navchange((string)obj));

        public SettingPageEs Sp {
            get {
                if (sp == null)
                {
                    sp = new SettingPageEs();
                }
                return sp;
            }
            set => sp = value; }

        public string IsSuperUser
        {

            get
            {
               return GlobalData.IsSuperUser()? "Visible" : "Hidden";
            }
        }

        public ResultPageEs Dw { get {
                if (dw == null)
                {
                    dw = new ResultPageEs();
                }
                return dw;
            } set => dw = value; }
        public AboutPage Ap { get {
                if (ap == null)
                {
                    ap = new AboutPage();
                }
                return ap;
            } set => ap = value; }

        public ChangePsw Cp { get
            {
                if (cp == null)
                    cp = new ChangePsw();
                return cp;
            }
             set => cp = value; }

        public HostPage Hp
        {
            get
            {
                if (hp == null)
                    hp = new HostPage();
                return                     hp ;
;
            }
            set => hp = value;
        }

        public void navchange(string obj)
        {
               int menu = int.Parse((string)obj);
            switch (menu)
            {
                case 1:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Up);
                    break;
                case 3:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Sp);
                    break;
                case 2:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Dw);
                    break;
                case 4:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Ap);
                    break;

                case 5:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Cp);
                    break;
                case 6:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Cor);
                    break;

                case 7:
                    DMSkin.Core.Broadcast.PushBroadcast("Navigation", Hp);
                    break;
            }
        }
    }



}
